package com.toe.forum.webapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.forum.service.ForumCategoryService;
import com.toe.forum.webapi.mapper.ForumCategoryMapper;
import com.toe.pojo.dto.forum.CategoryAddDTO;
import com.toe.pojo.dto.forum.CategoryUpdateDTO;
import com.toe.pojo.entity.forum.ForumCategory;
import com.toe.pojo.vo.forum.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章分类表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class ForumCategoryServiceImpl extends ServiceImpl<ForumCategoryMapper, ForumCategory> implements ForumCategoryService {

    @Autowired
    private ForumCategoryMapper forumCategoryMapper;

    @Override
    public void addCategory(CategoryAddDTO categoryAddDTO) {
        log.info("开始处理添加文章分类业务");
        log.info("获取添加的信息{}",categoryAddDTO);
        String forumName = categoryAddDTO.getForumName();
        log.info("获取添加的信息的name是{}",forumName);
        CategoryVO categoryVO = forumCategoryMapper.selectCategoryByForumName(forumName);
        log.info("判断该类别是否已经存在!");
        if (categoryVO!=null){
            log.error("该类别已经存在,请勿重复添加!!!");
            //该处的异常是乱加的,防止git代码冲突,后期统一添加吧!
            throw new ToeServiceException(StateEnum.ERR_CLIENT,"该类别已经存在,请勿重复添加!!!");
        }
        log.info("该类别不存在,可以添加!");
        Date now = new Date();
        ForumCategory forumCategory = new ForumCategory();
        forumCategory.setGmtCreate(now);
        forumCategory.setGmtModified(now);
        forumCategory.setForumName(forumName);
        forumCategoryMapper.insert(forumCategory);
    }

    @Override
    public void deleteCategoryById(Long id) {
        log.info("开始处理删除文章分类业务");
//        log.info("需要删除的文章分类的id是:{}",id);
//        log.info("查询该Id对应的分类是否存在");
        //此处也无需验证数据是否存在,如果数据不存在,受影响行数也为0
//        CategoryVO categoryVO = forumCategoryMapper.selectCategoryById(id);
//        if (categoryVO == null){
//            log.error("该类别不存在,无法删除!");
//            //该处的异常是乱加的,防止git代码冲突,后期统一添加吧!
//            throw new ToeServiceException(StateEnum.ERR_CLIENT,"该类别不存在,不能删除!");
//        }
        log.info("删除成功!");
        forumCategoryMapper.deleteById(id);
    }

    @Override
    public void updateCategoryById(CategoryUpdateDTO categoryUpdateDTO) {
        log.info("开始处理修改文章分类业务");
        //此处无需验证该类别是否存在,如不存在,受影响行数为0,无所谓
//        long id = categoryUpdateDTO.getId();
//        log.info("查询该Id对应的分类是否存在");
//        CategoryVO categoryVO = forumCategoryMapper.selectCategoryById(id);
//        if (categoryVO == null){
//            log.error("该类别不存在,无法修改!");
//            //该处的异常是乱加的,防止git代码冲突,后期统一添加吧!
//            throw new ToeServiceException(StateEnum.ERR_CLIENT,"该类别不存在,无法修改!");
//        }
        String forumName = categoryUpdateDTO.getForumName();
        log.info("查询该forumName对应的分类是否存在");
        CategoryVO categoryVO2 = forumCategoryMapper.selectCategoryByForumName(forumName);
        if (categoryVO2!=null){
            log.error("该类别已经存在,请勿重复添加!!!");
            //该处的异常是乱加的,防止git代码冲突,后期统一添加吧!
            throw new ToeServiceException(StateEnum.ERR_CLIENT,"该类别已经存在,请勿重复添加!!!");
        }
        log.info("修改完成!");
        forumCategoryMapper.updateCategoryById(categoryUpdateDTO);
    }

    @Override
    public List<CategoryVO> selectCategoryList() {
        log.info("开始执行查询全部文章分类业务!");
        List<CategoryVO> categoryVOS = forumCategoryMapper.selectCategory();
        return categoryVOS;
    }

    @Override
    public JsonPage<CategoryVO> selectCategoryList(Integer pageNum, Integer pageSize) {
        log.info("开始执行分页查询全部文章分类业务!");
        PageHelper.startPage(pageNum,pageSize);
        List<CategoryVO> categoryVOS = forumCategoryMapper.selectCategory();
        return JsonPage.restPage(new PageInfo<>(categoryVOS));
    }

    @Override
    public CategoryVO selectCategoryById(long id) {
        log.info("开始执行查询某个分类的业务");
        CategoryVO categoryVO = forumCategoryMapper.selectCategoryById(id);
        if (categoryVO==null){
            throw new ToeServiceException(StateEnum.ERR_CLIENT,"所查找分类不存在!");
        }
        return categoryVO;
    }
}
